package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.LayoutManager;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import rmi.Discussion;
import rmi.Me;

/**
 * @author REMY Timothée and CARTIER Arthur
 * 
 */
public class OneDiscussionTab extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3098524343601285624L;

	rmi.Discussion discussion;

	JLabel participantsList;

	JTextArea messageList;

	JTextArea newMessageField;

	JButton sendButton;

	JPanel bottomPanel;

	JScrollPane scrollPane;
	JScrollPane topScroll;

	int nextMessageToWrite;

	Me me;

	public OneDiscussionTab() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param isDoubleBuffered
	 */
	public OneDiscussionTab(final boolean isDoubleBuffered) {
		super(isDoubleBuffered);
		// TODO Auto-generated constructor stub
	}

	public OneDiscussionTab(final Discussion discussion, final Me me) {
		super(new BorderLayout());

		this.discussion = discussion;
		this.me = me;
		// creation of elements

		nextMessageToWrite = 0;
		messageList = new JTextArea();
		messageList.setEditable(false);
		participantsList = new JLabel();
		newMessageField = new JTextArea(4, 40);
		sendButton = new JButton("Send");
		sendButton.addActionListener(new SendButtonListener(this));

		scrollPane = new JScrollPane(newMessageField);
		topScroll = new JScrollPane(messageList);
		bottomPanel = new JPanel(new FlowLayout());

		// Adding elements in the bottomPanel
		bottomPanel.add(scrollPane);
		bottomPanel.add(sendButton);
		// adding elements in this Panel
		this.add(participantsList, BorderLayout.NORTH);
		this.add(topScroll, BorderLayout.CENTER);
		this.add(bottomPanel, BorderLayout.SOUTH);

	}

	/**
	 * @param layout
	 */
	public OneDiscussionTab(final LayoutManager layout) {
		super(layout);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param layout
	 * @param isDoubleBuffered
	 */
	public OneDiscussionTab(final LayoutManager layout,
			final boolean isDoubleBuffered) {
		super(layout, isDoubleBuffered);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return the discussion
	 */
	protected rmi.Discussion getDiscussion() {
		return discussion;
	}

	/**
	 * @return the me
	 */
	protected Me getMe() {
		return me;
	}

	/**
	 * @return the newMessageField
	 */
	protected JTextArea getNewMessageField() {
		return newMessageField;
	}

	/**
	 * @return the scrollPane
	 */
	protected JScrollPane getScrollPane() {
		return scrollPane;
	}

	/**
	 * @return the sendButton
	 */
	protected JButton getSendButton() {
		return sendButton;
	}

	public JScrollPane getTopScroll() {
		return topScroll;
	}

	/**
	 * @see javax.swing.JComponent#paint(java.awt.Graphics)
	 * @param g
	 */
	@Override
	public void paint(final Graphics g) {
		super.paint(g);
		setPreferredSize(new Dimension(getParent().getWidth(), getParent()
				.getHeight() - 40));
		scrollPane.setSize(new Dimension(getParent().getWidth() - 150, 100));

		if (nextMessageToWrite < discussion.getListMessages().size()) {
			final StringBuilder message = new StringBuilder();
			while ((nextMessageToWrite) < discussion.getListMessages().size()) {
				// creation of the MessageString

				message.append(discussion.getListMessages()
						.get(nextMessageToWrite).getAuthor());
				message.append(" :\n");
				message.append(discussion.getListMessages()
						.get(nextMessageToWrite).getMessage());
				message.append('\n');

				nextMessageToWrite++;
			}
			// adding the Message string to the textArea
			messageList.append(message.toString());
			messageList.repaint();
			topScroll.repaint();
		}
		participantsList.setText("There are "
				+ discussion.getParticipants().size()
				+ " participants in this discussion");
	}

	/**
	 * @param discussion
	 *            the discussion to set
	 */
	protected void setDiscussion(final rmi.Discussion discussion) {
		this.discussion = discussion;
	}

	/**
	 * @param me
	 *            the me to set
	 */
	protected void setMe(final Me me) {
		this.me = me;
	}

	/**
	 * @param newMessageField
	 *            the newMessageField to set
	 */
	protected void setNewMessageField(final JTextArea newMessageField) {
		this.newMessageField = newMessageField;
	}

	/**
	 * @param scrollPane
	 *            the scrollPane to set
	 */
	protected void setScrollPane(final JScrollPane scrollPane) {
		this.scrollPane = scrollPane;
	}

	/**
	 * @param sendButton
	 *            the sendButton to set
	 */
	protected void setSendButton(final JButton sendButton) {
		this.sendButton = sendButton;
	}

	public void setTopScroll(final JScrollPane topScroll) {
		this.topScroll = topScroll;
	}

}
